import "reflect-metadata"
// 通过InversifyExpressServer将express和inversify封装在一起
import { InversifyExpressServer } from "inversify-express-utils"
// 容器
import { Container } from "inversify"
import { User } from './src/user/controller'
import { UserService } from './src/user/services'
import express from 'express'
// import { PrismaClient } from "./generated/prisma"
import { PrismaDB } from './src/db'
import { PrismaClient } from "@prisma/client"
import { JWT } from './src/jwt'

const container = new Container()
// user模块
container.bind(User).to(User)
container.bind(UserService).to(UserService)
// 封装PrismaClient
container.bind<PrismaClient>('PrismaClient').toFactory(() => {
  return () => {
    return new PrismaClient()
  }
})
container.bind(PrismaDB).to(PrismaDB)
// jwt
container.bind(JWT).to(JWT)

const server = new InversifyExpressServer(container)
// 支持post
server.setConfig((app) => {
  app.use(express.json())
  // express关联jwt
  app.use(container.get(JWT).init())
})

const app = server.build()

app.listen(3000, () => {
  console.log("app is running at port 3000")
})
